package com.x25175.file.controller;

import com.x25175.file.Vo.ResponseVO;
import com.x25175.file.entity.Student;
import com.x25175.file.service.StudentService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @author 徐佳辉
 * @date 2020/11/21 17:02
 */
@Data
@Controller
@Slf4j
public class PictureController {

    @Autowired
    private StudentService studentService;
    @Value("${pictureurl}")
    private String url;


    /**
     * 上传或者更新图片
     *
     * @param file
     * @param studentId
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ResponseBody
    public ResponseVO upload(@RequestParam("file") MultipartFile file, Integer studentId) throws IOException {
        //上传到本地，url放到对应student表，返回url吧
        String fileName = file.getOriginalFilename();
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 新文件名
        fileName = studentId + suffixName;
        File dest = new File(url + fileName);

        Student student = studentService.selectByStudentId(studentId);
        if (student.getPicture() != null) {
            //获取图片路径 todo
            String picture = student.getPicture();
            log.info(picture);
            //磁盘路径
            String s = picture.replaceFirst("/img/", url);
            //删除磁盘图片
            boolean b = deleteDiskPict(s);
            //不管成不成功后面都要更新

        }
        // 传到本地
        if (dest.exists()) {

            file.transferTo(dest);
        } else {
            dest.mkdirs();
            file.transferTo(dest);

        }

        //更新数据库
        student.setUpdateTime(new Date());
        student.setPicture("/img/" + fileName);
        studentService.updateDynamic(student);

        String Url = "/img/" + fileName;
        //url在msg里面
        return ResponseVO.ok(Url);

    }

    /**
     * 删除图片
     *
     * @param s
     * @return
     */
    public boolean deleteDiskPict(String s) {
        File file = new File(s);
        boolean delete = file.delete();
        return delete;

    }

    /**
     * 删除图片
     *
     * @param studentId
     * @return
     * @throws IOException
     */
    @PostMapping("/delPic")
    @ResponseBody
    public ResponseVO upload(Integer studentId) throws IOException {

        log.info("studentId:{}", studentId);
        Student student = studentService.selectByStudentId(studentId);

        String picture = student.getPicture();
        log.info(picture);
        //磁盘路径
        String s = picture.replaceFirst("/img/", url);
        //删除磁盘图片
        boolean b = deleteDiskPict(s);
        student.setUpdateTime(new Date());
        student.setPicture(null);
        boolean b1 = studentService.updateDynamicExcluePic(student);
        return b1 ? ResponseVO.ok("删除成功") : ResponseVO.error("删除出现问题");


    }

}
